﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientManagement.Managers;

namespace PatientManagement
{
    public partial class FormPrescriptionManagement : Form
    {
        private int m_PatientId;
        public FormPrescriptionManagement(int patientId)
        {
            m_PatientId = patientId;
            InitializeComponent();
            dtgPrescriptions.AutoGenerateColumns = false;
            GetPrescriptionByPatientId();
        }

        public void GetPrescriptionByPatientId()
        {
            var prescriptions = ServiceManager.Instance.GetPrescriptionByPatientId(m_PatientId);
            prescriptions = prescriptions.OrderBy(x => x.Created).ToList();
            dtgPrescriptions.DataSource = prescriptions;
        }

        private void dtgPrescriptions_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == dtgPrescriptions.Columns["colViewDetails"].Index)
                {
                    var prescriptionId = Convert.ToInt32(dtgPrescriptions["colId", e.RowIndex].Value);
                    FormPrescription form = new FormPrescription(prescriptionId);
                    form.ShowDialog();
                }
                else if (e.ColumnIndex == dtgPrescriptions.Columns["colDelete"].Index)
                {
                    var prescriptionId = Convert.ToInt32(dtgPrescriptions["colId", e.RowIndex].Value);
                    var dialogResult = MessageBox.Show("Bạn có muốn xóa không", "Xác nhận", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        var result = ServiceManager.Instance.DeletePrescription(prescriptionId);
                        if (result)
                        {
                            GetPrescriptionByPatientId();
                        }
                        else
                        {
                            MessageBox.Show("Xóa thất bại");
                        }
                    }
                }

            }
        }
    }
}
